<template>
  <div class="container">
    <header class="header">
      <h1>第481期 我与软件有个约会</h1>
    </header>

    <!-- 前三名展示 -->
    <div class="top-three-container" v-if="topThree.length">
      <div class="top-three-item" v-for="(item, index) in topThree" :key="index" :class="{ 'left': index === 1, 'right': index === 2, 'center': index === 0 }">
        <div class="rank">{{ index + 1 }}名</div>
        <div class="image-container">
          <img :src="item.image" :alt="item.name">
        </div>
        <div class="info">
          <p class="name">{{ item.name }}</p>
          <p class="score">{{ item.score }}</p>
          <p class="points">{{ item.points }}</p>
        </div>
      </div>
    </div>

    <div class="table-container">
      <table class="table" id="rankTable">
        <thead>
          <tr>
            <th>排名</th>
            <th>头像</th>
            <th>姓名</th>
            <th>分数</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in sortedItems" :key="index">
            <td>{{ index + 1 }}</td>
            <td><img :src="item.image" :alt="item.name" width="50"></td>
            <td>{{ item.name }}</td>
            <td>{{ item.score }}</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request';

export default {
  data() {
    return {
      items: []
    };
  },
  computed: {
    sortedItems() {
      return this.items.slice().sort((a, b) => b.score - a.score);
    },
    topThree() {
      return this.sortedItems.slice(0, 3);
    }
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      try {
        request.get('/player/orderby').then(res => {
          if(res.code === 1){
            this.items = res.data;
          }else{
            alert("获取数据失败");
          }
        })
      } catch (error) {
        console.error('Error fetching data:', error);
      }
    }
  }
};
</script>

<style scoped>
body {
  font-family: Arial, sans-serif;
  line-height: 1.6;
  margin: 0;
  padding: 0;
  background-color: #f4f4f9;
}

.container {
  max-width: 800px;
  margin: 40px auto;
  padding: 20px;
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  justify-content: center;
}

.header h1 {
  font-size: 24px;
  text-align: center;
  margin: 0;
}

.top-three-container {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.top-three-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: calc(33% - 20px);
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.top-three-item.left {
  order: 1; /* 第二名靠左 */
}

.top-three-item.center {
  order: 2; /* 第一名居中 */
}

.top-three-item.right {
  order: 3; /* 第三名靠右 */
}

.rank {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
}

.image-container {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  overflow: hidden;
  margin-bottom: 10px;
}

.info {
  text-align: center;
}

.name {
  font-weight: bold;
  margin-bottom: 5px;
}

.score {
  margin-bottom: 5px;
}

.points {
  color: #888;
}

.table-container {
  width: 100%;
  max-height: 400px;
  overflow-y: auto;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  margin-top: 20px;
}

.table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 20px;
  background-color: transparent;
}

.table th,
.table td {
  padding: 10px;
  text-align: left;
  border-bottom: 1px solid #ddd;
}

.table th {
  background-color: #f2f2f2;
  position: sticky;
  top: 0;
  z-index: 1;
}

.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: #333;
  color: #fff;
  text-align: center;
  padding: 10px 0;
}

.footer button {
  margin: 0 10px;
  padding: 10px 20px;
  background-color: #555;
  color: #fff;
  border: none;
  cursor: pointer;
}

.footer button:hover {
  background-color: #777;
}
</style>